<template>
  <div class="content">
    <div class="header">
      <h3 class="title">{{ contentConfig?.header?.title ?? '数据列表' }}</h3>
      <el-button v-if="isCreate" type="primary" @click="handleNewUserClick">{{
        contentConfig?.header?.btnTitle ?? '新建'
      }}</el-button>
    </div>
    <div class="table">
      <!-- <ul v-if="userList">
        <li v-for="item in userList" :key="item.id">{{ item.name }}</li>
      </ul> -->
      <el-table
        :data="pageList"
        style="width: 100%"
        v-bind="contentConfig.childrenTree"
      >
        <template v-for="item in contentConfig.propsList" :key="item.prop">
          <template v-if="item.type === 'timer'">
            <el-table-column align="center" v-bind="item">
              <template #default="scoped">
                <!-- 时间数据格式化 -->
                {{ formatUTC(scoped.row[item.prop]) }}
              </template>
            </el-table-column>
          </template>
          <template v-else-if="item.type === 'handler'">
            <el-table-column align="center" v-bind="item">
              <template #default="scoped">
                <el-button
                  v-if="isUpdate"
                  icon="edit"
                  @click="handleEditeClick(scoped.row)"
                  type="primary"
                  text
                  >编辑</el-button
                >
                <el-button
                  v-if="isDelete"
                  @click="handleDeleteClick(scoped.row.id)"
                  icon="delete"
                  type="danger"
                  text
                  >删除
                </el-button>
              </template>
            </el-table-column>
          </template>
          <!-- <template v-if="item.type === 'custom'">
            <el-table-column align="center" v-bind="item">
              <template #default="scoped">
                <slot :name="item.slotName" v-bind="scoped" :prop="item.prop">
                </slot>
              </template>
            </el-table-column>
          </template> -->

          <template v-else>
            <el-table-column align="center" v-bind="item"> </el-table-column>
          </template>
        </template>
      </el-table>
    </div>
    <div class="pagenation">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[1, 2, 3]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import useSystemStore from '@/store/main/system/system'
import { storeToRefs } from 'pinia'
import { formatUTC } from '@/utils/format'
import usePermissions from '@/hooks/usePermissons'

interface IProps {
  contentConfig: {
    header?: {
      title?: string
      btnTitle?: string
    }
    propsList: any[]
    pageName: string
    childrenTree?: any
  }
}
// props有类型
const props = defineProps<IProps>()

const emit = defineEmits(['newClick', 'editClick'])

// 0，获取是否有按钮对应权限

const isCreate = usePermissions(`${props.contentConfig.pageName}:create`)
const isDelete = usePermissions(`${props.contentConfig.pageName}:delete`)

const isUpdate = usePermissions(`${props.contentConfig.pageName}:update`)

const isQuery = usePermissions(`${props.contentConfig.pageName}:query`)

const currentPage = ref(1)
const pageSize = ref(10)
const systemStore = useSystemStore()
// const queryInfo = ref({})
//1发送网络请求 异步的  传递查询参数
// systemStore.postUserListAction(queryInfo)
// 8 监听systemStore 中的actions
systemStore.$onAction(({ name, after }) => {
  after(() => {
    if (
      name === 'deletePageByIdAction' ||
      name === 'editPageDataAction' ||
      name === 'newPageDataAction'
    ) {
      currentPage.value = 1
    }
  })
})
// 一进来就调用
fetchPageListData()
// 第一次式空数据
// 2获取数据 做展示 解构  改成响应式的  要依赖一下计算属性 数据变化会再次展示
const { pageList, pageTotalCount } = storeToRefs(systemStore)
// console.log(userList)
// 3，页码相关的逻辑
function handleSizeChange() {
  // console.log('page-size', pageSize.value)
  fetchPageListData()
}
function handleCurrentChange() {
  // console.log('current-page', currentPage.value)
  fetchPageListData()
}
// 4，定义函数用于发送网络请求
function fetchPageListData(formData: any = {}) {
  if (!isQuery) return
  // 查询参数
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size //偏移量 相当于查询第几页的数据
  const pageInfo = { size, offset }
  const queryInfo = { ...pageInfo, ...formData }
  // 发送网络请求
  systemStore.postPageListAction(props.contentConfig.pageName, queryInfo)
}
// 5,删除
function handleDeleteClick(id: number) {
  // console.log('delete-click', id)
  // 删除完
  systemStore.deletePageByIdAction(props.contentConfig.pageName, id)
  // 重新请求数据我们也可以在store中调用方法
  // fetchUserListData({ offset: 0, size: 10 })
}
// 7,编辑
function handleEditeClick(itemData: any) {
  // console.log('edit-click', itemData)
  emit('editClick', itemData)
}
// 6新建用户
function handleNewUserClick() {
  // console.log('新建用户')
  emit('newClick')
}

// 暴露数据
defineExpose({ fetchPageListData })
</script>
<style lang="less" scoped>
.content {
  background-color: #fff;
  padding: 20px;
  margin-top: 20px;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 10px;
  .title {
    font-size: 22px;
  }
}
.el-table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }
  .el-button {
    margin: 0;
    padding: 5px 8px;
  }
}
.pagenation {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
